package com.jerry.springcloud.mapper;

import com.jerry.springcloud.bean.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

//这是Product操作数据库的mapper
@Mapper
public interface ProductMapper {

    @Select("select * from t_product")
    public List<Product> getProductList();

    @Select("select * from t_product where id=#{id}")
    public Product getProductById(String id);

    @Insert("insert into t_product(id, name, price, stock, description, icon, status, categoryType) values(#{id}, #{name}, #{price}, #{stock}, #{description}, #{icon}, #{status}, #{categoryType})")
    public int insertProduct(Product product);

    @Update("update t_product set name=#{name}, price=#{price}, stock=#{stock}, description=#{description}, icon=#{icon}, status=#{status}, categoryType=#{categoryType} where id=#{id}")
    public int updateProduct(Product product);

    @Delete("delete from t_product where id=#{id}")
    public int deleteProductById(Integer id);

    @Select("select * from t_product where status=#{status}")
    List<Product> getProductListByStatus(Integer status);

    /**
     * 注意：以下写法
     * 1，@Select后面的括号包含大括号
     * 2，使用<script>标签
     * 3，@Select后面大括号中的代码，每行后面使用逗号结束
     */
    @Select({
            "<script>",
            "select * from t_product where id in ",
            "<foreach collection='idList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<Product> getProductListByIdIn(@Param("idList") List<Integer> idList);
}
